/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BaixarExercicio.java
 *
 * Created on 12/07/2009, 08:50:18
 */
package interfaceGraficaUsuario;

import controle.BD;
import controle.DadosUsuarioEnum;
import controle.Sistema;
import controle.UsuariosEnum;
import java.awt.Color;
import java.util.List;
import users.Usuario;

/**
 *
 * @author João Paulo
 */
public class BaixarExercicio extends javax.swing.JFrame {

    /** Creates new form BaixarExercicio */
    public BaixarExercicio() {
        initComponents();
    }

    public BaixarExercicio(List<String> dadosUsuario, UsuariosEnum tipo) {
        initComponents();
        setTitle("Baixar exercicio");
        labelNotFound.setForeground(Color.RED);
        painelExibir.setEnabled(false);
        labelNotFound.setVisible(false);
        labelDownload.setVisible(false);
        if (tipo == UsuariosEnum.ALUNO) {
            campoID.setVisible(false);
            painelExibir.setText(Sistema.getSubmissoesAluno(dadosUsuario.get(DadosUsuarioEnum.LOGIN.ordinal())));
            botaoProcurarID.setVisible(false);
            labelID.setVisible(false);
        } else {
            campoID.setVisible(true);
            campoID.setText("0");
            labelID.setVisible(true);
            botaoProcurarID.setVisible(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelID = new javax.swing.JLabel();
        labelNotFound = new javax.swing.JLabel();
        campoID = new javax.swing.JTextField();
        botaoProcurarID = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        painelExibir = new javax.swing.JTextPane();
        labelIDExercicio = new javax.swing.JLabel();
        campoIDExercicio = new javax.swing.JTextField();
        botaoBaixarExercicio = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        labelDownload = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 600));

        labelID.setText("Informe o ID do usuário: ");

        labelNotFound.setText("Exercício não encontrado");

        botaoProcurarID.setText("Procurar");
        botaoProcurarID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoProcurarIDActionPerformed(evt);
            }
        });

        painelExibir.setBackground(new java.awt.Color(240, 240, 240));
        painelExibir.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        painelExibir.setSelectionColor(new java.awt.Color(0, 0, 0));
        jScrollPane1.setViewportView(painelExibir);

        labelIDExercicio.setText("Informe o ID do Exercício: ");

        botaoBaixarExercicio.setText("Baixar");
        botaoBaixarExercicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoBaixarExercicioActionPerformed(evt);
            }
        });

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        labelDownload.setText("DL");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelDownload, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(127, 127, 127)
                        .addComponent(labelNotFound, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(labelID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoID, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botaoProcurarID, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(labelIDExercicio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoIDExercicio, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(botaoBaixarExercicio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(botaoCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE))
                .addGap(43, 43, 43))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(labelNotFound)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelID)
                    .addComponent(campoID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoProcurarID))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelDownload)
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelIDExercicio)
                    .addComponent(campoIDExercicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoBaixarExercicio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoCancelar)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoProcurarIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoProcurarIDActionPerformed
        int idUsuario = Integer.parseInt(campoID.getText());
        if (procuraUsuario(idUsuario) != null) {
            Usuario usuario = procuraUsuario(idUsuario);
            painelExibir.setText(Sistema.listaDeExerciciosDoAluno(usuario.getLogin()));
            painelExibir.setVisible(true);
            labelNotFound.setVisible(false);
        } else {
            labelNotFound.setVisible(true);

        }
    }//GEN-LAST:event_botaoProcurarIDActionPerformed

    private void botaoBaixarExercicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoBaixarExercicioActionPerformed
        labelDownload.setText("Download completo: " + BD.getSubmissao(Integer.parseInt(campoIDExercicio.getText())).getCaminho());
        labelDownload.setVisible(true);
}//GEN-LAST:event_botaoBaixarExercicioActionPerformed

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_botaoCancelarActionPerformed

    /**
     * retorna o usuario
     * @param id identificaçao do usuario
     * @return usuario procurado pelo id
     */
    private Usuario procuraUsuario(int id) {
        return Sistema.getUsuario(id);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BaixarExercicio().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoBaixarExercicio;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoProcurarID;
    private javax.swing.JTextField campoID;
    private javax.swing.JTextField campoIDExercicio;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelDownload;
    private javax.swing.JLabel labelID;
    private javax.swing.JLabel labelIDExercicio;
    private javax.swing.JLabel labelNotFound;
    private javax.swing.JTextPane painelExibir;
    // End of variables declaration//GEN-END:variables
}
